import ctypes
import sys, os
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QIcon, QMovie
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTabWidget, QLabel

from arp_dep_test_layout import ArpDepTestLayout
from cpu_mem_monitor_layout import CpuMemMonitorLayout
from ssh_test_layout import SshTestLayout
from system_log_server_layout import SysLogServerLayout
from multi_dhcp_client_layout import MultiDhcpClientTestLayout
from mms_test_layout import MMSTestLayout

ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("leesen")


tab_layout_dict = {
    "Main": None,
    "Online-Host-Test": "ArpDepTestLayout()",
    "Multi-DHCP-Client": "MultiDhcpClientTestLayout()",
    "CPU-Mem-Monitor": "CpuMemMonitorLayout()",
    "SSH-Dep-Test": "SshTestLayout()",
    "SysLog-Server": "SysLogServerLayout()",
    "IEC61850-MMS-Test": "MMSTestLayout()"
}


def get_resource_path(filename):
    bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
    filepath = os.path.join(bundle_dir, filename)
    return filepath


def create_tab():
    layout = QVBoxLayout()
    label = QLabel("Undefined")
    label.setPixmap(QPixmap(get_resource_path('favicon.ico')))
    #item = QMovie(get_resource_path('bg1.gif'))
    #label.setMovie(item)
    #item.start()
    label.setAlignment(Qt.AlignCenter)
    layout.addWidget(label)

    return layout


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Network Test Tool @ LeeSen")
        self.setGeometry(100, 100, 1000, 650)

        main_layout = QVBoxLayout()

        self.tab_widget = QTabWidget()
        main_layout.addWidget(self.tab_widget)

        for key in tab_layout_dict:
            self.add_tab(key)

        central_widget = QWidget()
        central_widget.setLayout(main_layout)

        self.setCentralWidget(central_widget)

    def add_tab(self, name):
        tab = QWidget()
        if tab_layout_dict[name] is None:
            layout = create_tab()
        else:
            layout = eval(tab_layout_dict[name])
        tab.setLayout(layout)
        self.tab_widget.addTab(tab, name)


def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.setWindowIcon(QIcon(get_resource_path('favicon.ico')))
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
